package com.glp.controller;

import com.glp.common.constants.GlpConstants;
import com.glp.pojo.User;
import com.glp.pojo.dto.Menus;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("glp")
public class LoginController {

    @PostMapping("login")
    public String loginHTML(String loginName, String password, Model model){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
        try {
            subject.login(token);  //执行登录
            //登录成功后...
            User currentUser = (User) subject.getPrincipal();
            Session session = subject.getSession();
            currentUser.setPassword("");
            //设置用户session
            session.setAttribute("cur_user",currentUser);
            //设置菜单目录
            List<Menus> menus = generatorMenus(currentUser.getRoleId());
            //todo 封装list<list<menu>>  在导航栏展示
            session.setAttribute("menus",menus);
            return "redirect:/index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户未注册");
            return "login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }

    @GetMapping("logout")
    public String logout(){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }

    private List<Menus> generatorMenus(Integer roleId) {
        List<Menus> list = new ArrayList<>();
        Menus menus = new Menus();
        switch (roleId){
            case GlpConstants
                    .SYS_ROLE01:
                menus.setName("报告管理");
                List<Menus> list1_1 = new ArrayList<>();
                list1_1.add(new Menus("所有报告","/report/admin/list"));
                list1_1.add(new Menus("报告审核","/report/admin/list?status=1"));
                list1_1.add(new Menus("报告签发","/report/admin/list?status=2"));
                list1_1.add(new Menus("报告打印","/report/admin/list?status=4"));
                list1_1.add(new Menus("报告归档","/report/admin/list?status=7"));
                menus.setList(list1_1);
                Menus menus2 = new Menus();
                menus2.setName("用户管理");
                List<Menus> list1_2 = new ArrayList<>();
                list1_2.add(new Menus("添加用户","/admin/add-user-view"));
//                list1_2.add(new Menus("编辑用户","/admin/modify-user-view"));
                menus2.setList(list1_2);
                list.add(menus);
                list.add(menus2);
                break;
            case GlpConstants
                    .SYS_ROLE02:
                menus.setName("报告管理");
                List<Menus> list2_1 = new ArrayList<>();
                list2_1.add(new Menus("所有报告","/report/admin/list"));
                list2_1.add(new Menus("报告签发","/report/admin/list?status=2"));
                menus.setList(list2_1);
                list.add(menus);
                break;
            case GlpConstants
                    .SYS_ROLE03:
                menus.setName("报告管理");
                List<Menus> list3_1 = new ArrayList<>();
                list3_1.add(new Menus("所有报告","/report/admin/list"));
                list3_1.add(new Menus("报告审核","/report/admin/list?status=1"));
                menus.setList(list3_1);
                list.add(menus);
                break;
            case GlpConstants
                    .SYS_ROLE04:
                menus.setName("报告管理");
                List<Menus> l4_1 = new ArrayList<>();
                l4_1.add(new Menus("填写上传报告","/report/submit-view"));
                l4_1.add(new Menus("查看已保存报告","/report/client/save-list-view"));
                l4_1.add(new Menus("查看报告进度","/report/client/list-view"));
                menus.setList(l4_1);
                list.add(menus);
                break;
        }
        return list;
    }
}
